from concurrent.futures import ThreadPoolExecutor


# Task函数
def helper(i):
    print(f"helper: {i}")
    return i * i

if __name__ == '__main__':

    # 创建线程池
    thread_pool = ThreadPoolExecutor(max_workers=3)

    # 提交任务
    thread_pool.submit(helper, 1)
    thread_pool.map(helper, range(5))

    # Process.get()
    result = []
    for i in range(5):
        task = thread_pool.submit(helper, i)
        result.append(task)

    for task in result:
        print(task.result())


# 复用线程池（容易死锁）
def helper2(i):
    print(f"helper2: {i}")
    for j in range(i):
        thread_pool.submit(helper(j))

